<template>
	<div class="sys-org-container">
		<el-row :gutter="8" style="width: 100%">
			<el-col :span="4" :xs="24">
				<OrgTree ref="orgTreeRef" @node-click="nodeClick" />
			</el-col>

			<el-col :span="20" :xs="24">
				<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
					<el-form :model="state.queryParams" ref="queryForm" :inline="true">
						<el-form-item :label="$t('message.pageConfiguration.orgName')">
							<el-input v-model="state.queryParams.name" :placeholder="$t('message.pageConfiguration.orgName')" clearable />
						</el-form-item>
						<el-form-item :label="$t('message.pageConfiguration.orgCode')">
							<el-input v-model="state.queryParams.code" :placeholder="$t('message.pageConfiguration.orgCode')" clearable />
						</el-form-item>
						<el-form-item :label="$t('message.pageConfiguration.orgType')">
							<el-select v-model="state.queryParams.type" filterable clearable class="w100">
								<el-option v-for="item in state.orgTypeList" :key="item.value" :label="item.value" :value="item.code" />
							</el-select>
						</el-form-item>
						<el-form-item>
							<el-button-group>
								<el-button type="primary" icon="ele-Search" @click="handleQuery"> {{ $t('message.pageConfiguration.query') }} </el-button>
								<el-button icon="ele-Refresh" @click="resetQuery"> {{ $t('message.pageConfiguration.reset') }} </el-button>
							</el-button-group>
						</el-form-item>
						<el-form-item>
							<el-button type="primary" icon="ele-Plus" @click="openAddOrg" v-auth="'sysOrg:add'"> {{ $t('message.pageConfiguration.add') }} </el-button>
						</el-form-item>
					</el-form>
				</el-card>

				<el-card class="full-table" shadow="hover" style="margin-top: 8px">
					<el-table :data="state.orgData" style="width: 100%" v-loading="state.loading" row-key="id" default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border>
						<el-table-column prop="name" :label="$t('message.pageConfiguration.orgName')" header-align="center" show-overflow-tooltip />
						<el-table-column prop="code" :label="$t('message.pageConfiguration.orgCode')" align="center" show-overflow-tooltip />
						<el-table-column prop="level" :label="$t('message.pageConfiguration.level')" width="70" align="center" show-overflow-tooltip />
						<el-table-column prop="type" :label="$t('message.pageConfiguration.orgType')" align="center" :formatter="dictFormatter" show-overflow-tooltip />
						<el-table-column prop="orderNo" :label="$t('message.pageConfiguration.sort')" width="70" align="center" show-overflow-tooltip />
						<el-table-column :label="$t('message.pageConfiguration.status')" width="70" align="center" show-overflow-tooltip>
							<template #default="scope">
								<el-tag type="success" v-if="scope.row.status === 1">{{$t('message.pageConfiguration.enable')}}</el-tag>
								<el-tag type="danger" v-else>{{$t('message.pageConfiguration.disable')}}</el-tag>
							</template>
						</el-table-column>
						<el-table-column prop="createTime" :label="$t('message.pageConfiguration.createTime')" align="center" show-overflow-tooltip />
						<el-table-column prop="remark" :label="$t('message.pageConfiguration.remark1')" header-align="center" show-overflow-tooltip />
						<el-table-column :label="$t('message.pageConfiguration.operate')" width="140" fixed="right" align="center" show-overflow-tooltip>
							<template #default="scope">
								<el-button icon="ele-Edit" size="small" text type="primary" @click="openEditOrg(scope.row)" v-auth="'sysOrg:update'"> {{ $t('message.pageConfiguration.edit') }} </el-button>
								<el-button icon="ele-Delete" size="small" text type="danger" @click="delOrg(scope.row)" v-auth="'sysOrg:delete'"> {{ $t('message.pageConfiguration.del') }} </el-button>
							</template>
						</el-table-column>
					</el-table>
				</el-card>
			</el-col>
		</el-row>

		<EditOrg ref="editOrgRef" :title="state.editOrgTitle" :orgData="state.orgTreeData" @handleQuery="handleQuery" />
	</div>
</template>

<script lang="ts" setup name="sysOrg">
import { onMounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import OrgTree from '/@/views/system/org/component/orgTree.vue';
import EditOrg from '/@/views/system/org/component/editOrg.vue';

import { getAPI } from '/@/utils/axios-utils';
import { SysOrgApi, SysDictDataApi } from '/@/api-services/api';
import { SysOrg } from '/@/api-services/models';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();

const editOrgRef = ref<InstanceType<typeof EditOrg>>();
const orgTreeRef = ref<InstanceType<typeof OrgTree>>();
const state = reactive({
	loading: false,
	orgData: [] as Array<SysOrg>, // 机构列表数据
	orgTreeData: [] as Array<SysOrg>, // 机构树所有数据
	queryParams: {
		id: 0,
		name: undefined,
		code: undefined,
		type: undefined,
	},
	editOrgTitle: '',
	orgTypeList: [] as any,
});

onMounted(async () => {
	handleQuery();

	let resDicData = await getAPI(SysDictDataApi).apiSysDictDataDataListCodeGet('org_type');
	state.orgTypeList = resDicData.data.result;
});

// 查询操作
const handleQuery = async (updateTree: boolean = false) => {
	state.loading = true;
	var res = await getAPI(SysOrgApi).apiSysOrgListGet(state.queryParams.id, state.queryParams.name, state.queryParams.code, state.queryParams.type);
	state.orgData = res.data.result ?? [];
	state.loading = false;
	// 是否更新左侧机构列表树
	if (updateTree == true) {
		orgTreeRef.value?.initTreeData();
	}

	// 若无选择节点并且查询条件为空时，更新编辑页面机构列表树
	if (state.queryParams.id == 0 && state.queryParams.name == undefined && state.queryParams.code == undefined && state.queryParams.type == undefined) state.orgTreeData = state.orgData;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.id = 0;
	state.queryParams.name = undefined;
	state.queryParams.code = undefined;
	state.queryParams.type = undefined;
	handleQuery();
};

// 打开新增页面
const openAddOrg = () => {
	state.editOrgTitle = t('message.pageConfiguration.addOrg');
	editOrgRef.value?.openDialog({ status: 1, orderNo: 100 });
};

// 打开编辑页面
const openEditOrg = (row: any) => {
	state.editOrgTitle = t('message.pageConfiguration.editOrg');
	editOrgRef.value?.openDialog(row);
};

// 删除
const delOrg = (row: any) => {
	ElMessageBox.confirm(t('message.pageConfiguration.confirmDelOrg')+`：【${row.name}】?`, t('message.messages.confirmDelOrg'), {
		confirmButtonText:t('message.messages.confirm'),
		cancelButtonText: t('message.messages.cancel'),
		type: 'warning',
	})
		.then(async () => {
			await getAPI(SysOrgApi).apiSysOrgDeletePost({ id: row.id });
			ElMessage.success(t('message.messages.deleteSuccess'));
			handleQuery(true);
		})
		.catch(() => {});
};

// 树组件点击
const nodeClick = async (node: any) => {
	state.queryParams.id = node.id;
	state.queryParams.name = undefined;
	state.queryParams.code = undefined;
	state.queryParams.type = undefined;
	handleQuery();
};

// 字典转换
const dictFormatter = (row: any, column: any, cellValue: any) => {
	return state.orgTypeList.find((u: any) => u.code == cellValue)?.value;
};
</script>
